<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin\Collection\CollectionResumeCheck;
use App\Models\Common\CollOption;
use App\Models\Personal\PersonalUser;
use App\Models\Personal\Resume;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Style;

class SmartRefreshResumeController extends CommonController{
    /**
     * 智能刷新简历
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request){
        $input = $request->only('keyword', 'inside', 'exceed');
        $keyword = trim($request->keyword);
        $inside = (!empty($request->inside) && is_numeric($request->inside)) ? $request->inside : '';
        $exceed = (!empty($request->exceed) && is_numeric($request->exceed)) ? $request->exceed : '';

        $resume = DB::table('personal_users as t1')
            ->join('resume as t2', 't1.uid', '=', 't2.uid')
            ->join('collection_resume_check as t3', 't2.id', '=', 't3.resume_id')
            ->select(
                't1.uid',
                't2.name', 't2.birthday', 't2.education', 't2.intentionjobs', 't2.mobile', 't2.id', 't2.updatetime',
                't3.created_at'
            )->where(function($query) use ($keyword){
                if(strlen($keyword) > 0){
                    $query->where('t2.name', 'like', '%' . $keyword . '%')
                        ->orWhere('t2.mobile', 'like', '%' . $keyword . '%');
                }
            })->where(function($query) use ($inside, $exceed){
                //几天内
                if(!empty($inside)){
                    $query->where('t2.updatetime', '>', date('Y-m-d H:i:s', strtotime('-' . $inside . ' days')));
                }
                //超过几天
                if(!empty($exceed)){
                    $query->where('t2.updatetime', '<', date('Y-m-d H:i:s', strtotime('-' . $exceed . ' days')));
                }
            })
            ->orderBy('t3.created_at', 'desc')
            ->paginate(20);
        foreach($resume as $v){
            $v->education = ($v->education) ? CollOption::getOptionName($v->education, 'education') : '初中';
            $v->updatetime = timeTranx($v->updatetime);
            $v->created_at = timeTranx($v->created_at);
        }
        //分页分配参数 过滤boolean假值
        $appends = arrayFilter($input);
        return Style::view('admin.smart_refresh_resume.index', compact('resume', 'appends'));
    }

    /**
     * 智能刷新简历
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function smartUpdate(Request $request){
        $resume_id = $request->input('selected_id.*');
        foreach($resume_id as $k => $v){
            $collectionResumeCheck = CollectionResumeCheck::where('resume_id', $v)->select('created_at')->first();
            if(!empty($collectionResumeCheck)){
                $result = Resume::where('id', $v)->update(['updatetime' => $collectionResumeCheck->created_at, 'xs_status' => 0]);
                if(!empty($result)){
                    CollectionResumeCheck::where('resume_id', $v)->delete();
                }
            }
        }
        flash('刷新成功', 'success');
        return redirect()->back();
    }
}
